import {Message} from 'iview';
import filterGroupService from '../apis/filtergroup';

const SET_FILTER_GROUP = 'SET_FILTER_GROUP';

const state = {
    filterGroupRepository: {}
};
const getters = {};
const actions = {
    getFilterGroup({commit, state}, payload) {
        return filterGroupService.getFilterGroupApi(payload.formId).then(res => {
            if (res.data.code !== 0) {
                Message.error(res.data.msg);
                throw new Error(res.data.msg);
            } else {
                return res.data.data;
            }
        }).then(data => {
            commit(SET_FILTER_GROUP, {
                formId: payload.formId,
                list: data
            });
            if (payload.success !== undefined) {
                payload.success();
            }
        }).catch(err => {
            console.log(err);
        });
    },
    createFilterGroup({commit, state}, payload) {
        return filterGroupService.createFilterGroupApi({
            formId: payload.formId,
            info: payload.info
        }).then(res => {
            if (res.data.code !== 0) {
                Message.error(res.data.msg);
                if (payload.error !== undefined) {
                    payload.error();
                }
                throw new Error(res.data.msg);
            } else {
                return res.data.data;
            }
        }).then(data => {
            let filterGroup = state.filterGroupRepository[payload.formId].concat();
            filterGroup.push(data);
            commit(SET_FILTER_GROUP, {
                formId: payload.formId,
                list: filterGroup
            });
            if (payload.success !== undefined) {
                payload.success(data);
            }
        }).catch(err => {
            console.log(err);
        });
    },
    updateFilterGroup({commit, state}, payload) {
        return filterGroupService.updateFilterGroupApi({
            filterId: payload.filterId,
            info: payload.info
        }).then(res => {
            if (res.data.code !== 0) {
                Message.error(res.data.msg);
                if (payload.error !== undefined) {
                    payload.error();
                }
                throw new Error(res.data.msg);
            } else {
                return res.data.data;
            }
        }).then(data => {
            let list = state.filterGroupRepository[payload.formId].concat();
            list = list.map(value => {
                if (value.id == data.id) {
                    return data;
                }

                return value;
            });
            commit(SET_FILTER_GROUP, {
                formId: payload.formId,
                list: list
            });
            if (payload.success !== undefined) {
                payload.success(data);
            }
        }).catch(err => {
            console.log(err);
        });
    },
    deleteFilterGroup({commit, state}, payload) {
        return filterGroupService.deleteFilterGroupApi(payload.filterId).then(res => {
            if (res.data.code !== 0) {
                Message.error(res.data.msg);
                if (payload.error !== undefined) {
                    payload.error();
                }
                throw new Error(res.data.msg);
            } else {
                return res.data.data;
            }
        }).then(data => {
            commit(SET_FILTER_GROUP, {
                formId: payload.formId,
                list: data
            });
            if (payload.success !== undefined) {
                payload.success(data);
            }
        }).catch(err => {
            console.log(err);
        });
    }
};
const mutations = {
    [SET_FILTER_GROUP](state, {formId, list}) {
        state.filterGroupRepository[formId] = list;
    }
};

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}